package fact.view.controller;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import fact.view.DTO.MakeDTO;
import fact.view.DTO.MarketDTO;
import fact.view.DTO.ResponsibilityDTO;
import fact.view.DTO.UserDTO;
import fact.view.model.MakeModelBean;
import fact.view.model.MarketModelBean;
import fact.view.model.ResponsibilityModelBean;
import fact.view.model.UserModelBean;
import fact.view.util.FacesUtils;
import fact.view.util.ServletUtil;


@ManagedBean(name = "responsibilityBean")
@RequestScoped
public class ResponsibilityBean extends ServletUtil implements Serializable {

	private static final long serialVersionUID = 8292853620260831727L;
	private boolean editOpened = false;
	
	
	@ManagedProperty(value="#{responsibilityModelBean}")
	private ResponsibilityModelBean responsibilityModelBean;
	
	@ManagedProperty(value="#{makeModelBean}")
	private MakeModelBean makeModelBean;
	
	@ManagedProperty(value="#{marketModelBean}")
	private MarketModelBean marketModelBean;
	
	@ManagedProperty(value="#{userModelBean}")
	private UserModelBean userModelBean;
	
	private String userId;
	
	
	ArrayList<ResponsibilityDTO> respList=new  ArrayList<ResponsibilityDTO>();

	private ResponsibilityDTO resp=new ResponsibilityDTO();
	private ArrayList<MakeDTO> allMakeList;
	private String[] assignedMakeList;
	private ArrayList<MarketDTO> allMarketList;
	private String[] assignedMarketList;
	private UserDTO editUser=new UserDTO();
	
	
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    
	
    public ArrayList<ResponsibilityDTO> getRespList() throws SQLException {
		
		try{
			
			if(session.getAttribute("session_responsiblityList")==null)
			{
				respList=responsibilityModelBean.getResponsiblityList();
				session.setAttribute("session_responsiblityList", respList);
			}
			else
				respList=(ArrayList<ResponsibilityDTO>)session.getAttribute("session_responsiblityList");
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return respList;
		
	}
    

    public String updateResponsibility(ActionEvent event) throws SQLException
	{
		editOpened=false;
		
		if(this.editUser.getUserId()==null || this.editUser.getUserId().trim().equalsIgnoreCase(""))
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:userId","Please Select a User.");
		}
		if(this.editUser.getUserRoleId()==0)
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:userRoleId","Please Select a Role.");
		}

		
		for(int i=0;i<assignedMakeList.length;i++)
		{
			System.out.print(assignedMakeList[i]);
		}
		
		
		if(editOpened==false)
		{
			
			
				if(responsibilityModelBean.addResponsibility(editUser,assignedMakeList,assignedMarketList)==true)
				{
					respList=responsibilityModelBean.getResponsiblityList();
					session.setAttribute("session_responsiblityList", respList);
					
					FacesUtils.addInfoMessage(null,"Responsibility has successfully been added.");
					return "admin/responsibility.jsf";
	
				}
				
		}
		return null;
	}
    
	
    public String addResponsibility(ActionEvent event) throws SQLException
	{
    	editOpened=false;
		
		if(this.editUser.getUserId()==null || this.editUser.getUserId().trim().equalsIgnoreCase(""))
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:userId","Please Select a User.");
		}
		if(this.editUser.getUserRoleId()==0)
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:userRoleId","Please Select a Role.");
		}

		
		for(int i=0;i<assignedMakeList.length;i++)
		{
			System.out.print(assignedMakeList[i]);
		}
		
		
		if(editOpened==false)
		{
			
			
				if(responsibilityModelBean.addResponsibility(editUser,assignedMakeList,assignedMarketList)==true)
				{
					respList=responsibilityModelBean.getResponsiblityList();
					session.setAttribute("session_responsiblityList", respList);
					
					FacesUtils.addInfoMessage(null,"Responsibility has successfully been added.");
					return "admin/responsibility.jsf";
	
				}
				
		}
		return null;
	}
    
    
	public ResponsibilityModelBean getResponsibilityModelBean() {
		return responsibilityModelBean;
	}


	public void setResponsibilityModelBean(
			ResponsibilityModelBean responsibilityModelBean) {
		this.responsibilityModelBean = responsibilityModelBean;
	}


	public MakeModelBean getMakeModelBean() {
		return makeModelBean;
	}


	public void setMakeModelBean(MakeModelBean makeModelBean) {
		this.makeModelBean = makeModelBean;
	}


	public MarketModelBean getMarketModelBean() {
		return marketModelBean;
	}


	public void setMarketModelBean(MarketModelBean marketModelBean) {
		this.marketModelBean = marketModelBean;
	}


	public UserModelBean getUserModelBean() {
		return userModelBean;
	}


	public void setUserModelBean(UserModelBean userModelBean) {
		this.userModelBean = userModelBean;
	}


	public String getUserId() {
		return userId;
	}


	public void setUserId(String userId) {
		this.userId = userId;
	}


	public ResponsibilityDTO getResp() {
		return resp;
	}


	public void setResp(ResponsibilityDTO resp) {
		this.resp = resp;
	}


	public ArrayList<MakeDTO> getAllMakeList() throws SQLException {
		
		if(session.getAttribute("session_allMakeList")==null)
		{
			allMakeList=makeModelBean.getMakeList();
			session.setAttribute("session_allMakeList", allMakeList);
		}
		else
			allMakeList=(ArrayList<MakeDTO>)session.getAttribute("session_allMakeList");
		
		return allMakeList;

	}


	public void setAllMakeList(ArrayList<MakeDTO> allMakeList) {
		this.allMakeList = allMakeList;
	}


	public String[] getAssignedMakeList() throws SQLException {
		
		if(session.getAttribute("session_userMakeList"+this.userId)==null)
		{
			assignedMakeList=makeModelBean.getMakeList(this.userId);
			session.setAttribute("session_userMakeList", assignedMakeList);
		}
		else
			assignedMakeList=(String[])session.getAttribute("session_userMakeList"+this.userId);
		
		return assignedMakeList;

	}


	public void setAssignedMakeList(String[] assignedMakeList) {
		this.assignedMakeList = assignedMakeList;
	}


	public ArrayList<MarketDTO> getAllMarketList() throws SQLException {
		
		
		if(session.getAttribute("session_allMarketList")==null)
		{
			allMarketList=marketModelBean.getMarketList();
			session.setAttribute("session_allMarketList", allMarketList);
		}
		else
			allMarketList=(ArrayList<MarketDTO>)session.getAttribute("session_allMarketList");
		
		return allMarketList;
	}


	public void setAllMarketList(ArrayList<MarketDTO> allMarketList) {
		this.allMarketList = allMarketList;
	}


	public String[] getAssignedMarketList() throws SQLException {
		if(session.getAttribute("session_userMarketList"+this.userId)==null)
		{
			assignedMarketList=marketModelBean.getMarketList(this.userId);
			session.setAttribute("session_userMarketList", assignedMarketList);
		}
		else
			assignedMarketList=(String[])session.getAttribute("session_userMarketList"+this.userId);
		
		return assignedMarketList;
	}


	public void setAssignedMarketList(String[] assignedMarketList) {
		this.assignedMarketList = assignedMarketList;
	}


	


	public UserDTO getEditUser() {
		return editUser;
	}


	public void setEditUser(UserDTO editUser) {
		this.editUser = editUser;
	}


	public void setRespList(ArrayList<ResponsibilityDTO> respList) {
		this.respList = respList;
	}


	/** -- Popup Modal Event ---**/
		public boolean isEditOpened() { return editOpened; }
	    
	    public void setEditOpened(boolean editOpened) { this.editOpened = editOpened; }
	    
	    
	    public void openEditEvent(ActionEvent event)  throws SQLException{
	    	FacesContext fc = FacesContext.getCurrentInstance();
	    	this.userId = getRequestParam(fc,"userId");
	    	
	    	if(session.getAttribute("session_responsibilityUser"+this.userId)==null)
			{
				this.editUser=userModelBean.getUser(this.userId);
				session.setAttribute("session_responsibilityUser"+this.userId, editUser);
			}
			else
				this.editUser=(UserDTO)session.getAttribute("session_responsibilityUser"+this.userId);
		
	    	editOpened = true;
	    }
	    
	    
	    public void closeEvent(ActionEvent event) {
	        editOpened = false;
	    }

    /*  End of Modal */
	

}